home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / PCI Driver Development Kit / • Tools / Utility / PCISlots / Sources / PCISlotsUtils.cp < prev   
Encoding:
Text File  |  1996-08-20  |  13.7 KB  |  557 lines  |  [TEXT/MPS ]

  1. // Copyright © 1995 by Apple Computer, Inc.  All rights reserved.
  2. // PCISlotsUtils.cp
  3.  
  4. /*
  5. FILE
  6.     PCISlotsUtils.cp
  7.  
  8. NAME
  9.     PCI Slot Peek utility source file
  10.  
  11. DESCRIPTION
  12.     This source file implements misc. utility functions used by the
  13.     PCI Slot Peek application. It provides the 68K->PPC glue code
  14.     to the NameRegistry library.
  15.  
  16. MODIFICATION HISTORY
  17.     Created by Terry Teague
  18.  
  19.     15 May 95    -    TRT    -    Initial version
  20.     17 Jul 95    -    TRT    -    Tidy-up source for PPCC v1.0.5, Universal Headers v2.0 final,
  21.                                     CodeWarrior 6.1. Add support for finding slot # given driver refNum.
  22.  
  23. =============================================================================================
  24. */
  25.  
  26. //================================================================================================
  27. // Build-time #defines
  28. //================================================================================================
  29.  
  30. // pre-Universal Headers (<MPW 3.3.1) compatibility
  31. #ifndef    qPreUniHeaders
  32. #define    qPreUniHeaders        0
  33. #endif
  34.  
  35. //================================================================================================
  36. // Standard Includes
  37. //================================================================================================
  38.  
  39. #include <Types.h>
  40. #include <Packages.h>
  41. #include <Dialogs.h>
  42. #include <Memory.h>
  43. //#if    qPreUniHeaders
  44. //#include <OSEvents.h>
  45. //#endif
  46. #include <QuickDraw.h>
  47. #include <Resources.h>
  48. #include <ToolUtils.h>
  49. #include    <StdIO.h>
  50. #include    <Slots.h>
  51. #include    <ROMDefs.h>
  52. #include    <Devices.h>
  53. #include    <Traps.h>
  54. #include    <String.h>
  55. #include    <Strings.h>
  56. #include    <CType.h>
  57. #if    qPreUniHeaders
  58. #include    <SysEqu.h>
  59. #else
  60. #include    <LowMem.h>
  61. #endif
  62. #include    <Power.h>
  63. #include    <DeskBus.h>
  64.  
  65. // v2.0a3 or earlier
  66. //#ifndef    __GESTALTEQU__
  67. //#include    <GestaltEqu.h>
  68. //#endif
  69.  
  70. // v2.0 final or later
  71. #ifndef    __GESTALT__
  72. #include    <Gestalt.h>
  73. #endif
  74.  
  75. // v2.0 final or later
  76. #ifndef __CODEFRAGMENTS__
  77. #include    <CodeFragments.h>
  78. #endif
  79.  
  80. // v2.0a3 or earlier (also kept in v2.0 final for compatibility)
  81. //#ifndef __FRAGLOAD__
  82. //#include    <FragLoad.h>
  83. //#endif
  84.  
  85. #ifndef __MIXEDMODE__
  86. #include    <MixedMode.h>
  87. #endif
  88.  
  89. // v2.0a3 or earlier
  90. #ifdef    __FRAGLOAD__
  91. typedef SInt32 OSStatus;
  92. #endif
  93.  
  94. #ifndef    __NAMEREGISTRY__
  95. #include    <NameRegistry.h>
  96. #endif
  97.  
  98. //================================================================================================
  99. // PCISlots Specific Includes
  100. //================================================================================================
  101.  
  102. #ifndef    __PCISLOTS__
  103. #include    "PCISlots.h"
  104. #endif
  105.  
  106. #ifdef __cplusplus
  107. extern "C" {
  108. #endif
  109.  
  110. #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
  111. #pragma options align=mac68k
  112. #endif
  113.  
  114. #ifdef __CFM68K__
  115. #pragma lib_export on
  116. #endif
  117.  
  118. Boolean FormatStringProperty(
  119.         RegPropertyValueSize    propertySize,
  120.         const void                *propertyValue,
  121.         char                        *formatStr,
  122.         char                        *result)
  123. {
  124.         Boolean                    isStringProperty;
  125.         Boolean                    isAscii;
  126.         int                        i;
  127.         register char            *cp;
  128.         
  129.         isStringProperty = FALSE;
  130.         
  131.         if (propertySize < (kOneLineFormatLength - 3))
  132.         {
  133.             isAscii = TRUE;
  134.             cp = ((char *) propertyValue);
  135.             for (i = 0; isAscii && i < (propertySize - 1); i++)
  136.                 isAscii = IS_ASCII_PRINT(cp[i]);
  137.             if (isAscii && cp[i] == '\0')
  138.             {
  139.                 //sprintf(result, "\"%s\"", cp);
  140.                 sprintf(result, formatStr, cp);
  141.                 isStringProperty = TRUE;
  142.             }
  143.         }
  144.         
  145.         return (isStringProperty);
  146. }
  147.  
  148. Boolean FormatSmallProperty(
  149.         RegPropertyValueSize    propertySize,
  150.         const void                *propertyValue,
  151.         char                        *formatStr,
  152.         char                        *result)
  153. {
  154.         Boolean                isSmallProperty;
  155.         long                    *lp;
  156.         short                    *sp;
  157.         
  158.         isSmallProperty = FALSE;
  159.         
  160.         if (propertySize == sizeof (short))
  161.         {
  162.             sp = (short *) propertyValue;
  163.             //sprintf(result, "[%04x = %d]", ((int) *sp) & 0xFFFF, (int) *sp);
  164.             sprintf(result, formatStr, ((int) *sp) & 0xFFFF);
  165.             isSmallProperty = TRUE;
  166.         }
  167.         
  168.         if (propertySize == sizeof (long))
  169.         {
  170.             lp = (long *) propertyValue;
  171.             //sprintf(result, "[%08x = %ld]", lp[0], lp[0]);
  172.             sprintf(result, formatStr, lp[0]);
  173.             isSmallProperty = TRUE;
  174.         } else
  175.         
  176.         if (propertySize == (sizeof (long) * 2))
  177.         {
  178.             lp = (long *) propertyValue;
  179.             //sprintf(result, "[%08x = %ld], [%08x = %ld]", lp[0], lp[0], lp[1], lp[1]);
  180.             sprintf(result, formatStr, lp[0], lp[1]);
  181.             isSmallProperty = TRUE;
  182.         }
  183.         
  184.         return (isSmallProperty);
  185. }
  186.  
  187. /*
  188.  * EnumeratePropertiesForThisName finds selected properties for the current path,
  189.  * based on info stored in a STR# resource, and then displays those properties.
  190.  */
  191. void EnumeratePropertiesForThisName(
  192.         RegEntryID                    *entryID,
  193.         const RegCStrPathName    *pathName)
  194. {
  195.         OSStatus                    status;
  196.         Boolean                    done;
  197.         RegPropertyNameBuf    foundProperty;
  198.         RegPropertyValueSize    propertySize;
  199. //        RegPropertyModifiers propertyModifiers;
  200.         void                        *propertyValue;
  201.         short                        whichProperty;
  202.         
  203.         done = FALSE;
  204.         
  205.         for (whichProperty = 1; !done; whichProperty++)
  206.         {
  207.             // get the next property that we are interested in
  208.             
  209.             getindstring((char *)foundProperty, kPropertyNameSTRID, whichProperty);
  210.             done = foundProperty[0] == '\0';    // no more properties found in STR# resource
  211.             
  212.             if (!done)
  213.             {
  214.                 propertyValue = NULL;
  215.                 propertySize = 0;
  216.                 status = myRegistryPropertyGetSize(entryID, foundProperty, &propertySize);
  217.                 
  218.                 if (status == noErr)
  219.                 {
  220.                     propertyValue = NewPtr(propertySize);
  221.                     
  222.                     if (propertyValue == NULL)
  223.                     {
  224.                         status = MemError();
  225.                         //DebugStr((const unsigned char*)"\pNo memory to store property");
  226.                     }
  227.                 }
  228.                 
  229.                 if (status == noErr)
  230.                 {
  231.                     status = myRegistryPropertyGet(entryID, foundProperty, propertyValue, &propertySize);
  232.                 }
  233.                 
  234. #if 0
  235.                 if (status == noErr)
  236.                 {
  237.                     status = myRegistryPropertyGetMod(entryID, foundProperty, &propertyModifiers);
  238.                 }
  239. #endif
  240.                 
  241.                 if (status == noErr)
  242.                 {
  243.                     char            tempstr[256],
  244.                                     tempstr1[256];
  245.                     
  246.                     getindstring(tempstr, kDisplayPropertyValueSTRID, whichProperty);
  247.                     
  248.                     if (!FormatStringProperty(propertySize, propertyValue, tempstr, tempstr1))
  249.                         FormatSmallProperty(propertySize, propertyValue, tempstr, tempstr1);
  250.                     
  251.                     //sprintf(tempstr2, tempstr, tempstr1);    // formatting string
  252.                     printf("%s\n", tempstr1);
  253.                 }
  254.                 
  255.                 if (propertyValue != NULL)
  256.                     DisposePtr((Ptr) propertyValue);
  257.             }
  258.         }
  259.         
  260.         printf("\n");
  261. }
  262.  
  263. /*
  264.  * GetSlotForThisName returns the "AAPL,slot-name" property (if valid) for the current path.
  265.  */
  266. OSErr GetSlotForThisName(
  267.         RegEntryID                    *entryID,
  268.         const RegCStrPathName    *pathName)
  269. {
  270.         OSStatus                    status;
  271.         RegPropertyNameBuf    foundProperty;
  272.         RegPropertyValueSize    propertySize;
  273.         void                        *propertyValue;
  274.  
  275.         propertyValue = NULL;
  276.         propertySize = 0;
  277.         getindstring((char *)foundProperty, kSlotPropertySTRID, kSlotPropertyName);
  278.         //strcpy(foundProperty, "AAPL,slot-name");
  279.         
  280.         status = myRegistryPropertyGetSize(entryID, foundProperty, &propertySize);
  281.         
  282.         if (status == noErr)
  283.         {
  284.             propertyValue = NewPtr(propertySize);
  285.             
  286.             if (propertyValue == NULL)
  287.             {
  288.                 status = MemError();
  289.                 //DebugStr((const unsigned char*)"\pNo memory to store property");
  290.             }
  291.         }
  292.         
  293.         if (status == noErr)
  294.         {
  295.             status = myRegistryPropertyGet(entryID, foundProperty, propertyValue, &propertySize);
  296.                     
  297.             // Slot names on Power Macintosh currently are of form : A1, B1, C1, D2, E2, F2
  298.             // but older versions of drivers create slot-names like "mace" which are not what we are looking for
  299.             
  300.             if ((status == noErr) && (propertySize > 4))    // could check if slot name contains non-Hex digits
  301.                 status = -1;    // doesn't appear to be a valid slot name
  302.         }
  303.         
  304.         if (status == noErr)
  305.         {
  306.             char        tempstr[256];
  307.                         
  308.             getindstring(tempstr, kSlotPropertySTRID, kSlotPropertyValue);    // formatting string
  309.             printf(tempstr, (char *)propertyValue, pathName);
  310.             printf("\n");
  311.         }
  312.         
  313.         if (propertyValue != NULL)
  314.             DisposePtr((Ptr) propertyValue);
  315.             
  316.         return (status);
  317. }
  318.  
  319. /*
  320.  * This is adapted from the sample in the 8/3/94 PCI Drivers draft. Enumerate all
  321.  * paths and all properties for each path.
  322.  */
  323. short EnumerateNameRegistry(void)
  324. {
  325.         OSStatus                    status;
  326.         RegEntryIter            cookie;
  327.         Boolean                    done;
  328.         RegEntryIterationOp    op;
  329.         RegEntryID                entry;
  330.         RegCStrPathName        *pathName;
  331.         RegPathNameSize        pathNameSize;
  332.         short                        numSlots;
  333.         
  334.         numSlots = 0;
  335.         op = kRegIterContinue;
  336.         status = myRegistryEntryIterateCreate(&cookie);
  337.         
  338.         if (status == noErr)
  339.         {
  340.             done = FALSE;
  341.             
  342.             while (status == noErr && done == FALSE)
  343.             {
  344.                 status = myRegistryEntryIterate(&cookie, op, &entry, &done);
  345.                 
  346.                 if (status == noErr && done == FALSE)
  347.                 {
  348.                     pathName = NULL;
  349.                     status = myRegistryEntryToPathSize(&entry, &pathNameSize);
  350.                     
  351.                     if (status == noErr)
  352.                     {
  353.                         pathName = (RegCStrPathName *) NewPtr(pathNameSize);
  354.                         if (pathName == NULL)
  355.                             status = MemError();
  356.                     }
  357.                     
  358.                     if (status == noErr)
  359.                     {
  360.                         status = myRegistryCStrEntryToPath(&entry, pathName, pathNameSize);
  361.                         //printf("%s\n", pathName);    // debugging
  362.                     }
  363.                     
  364.                     if (status == noErr)
  365.                     {
  366.                         status = GetSlotForThisName(&entry, pathName);
  367.                         
  368.                         if (!status)
  369.                         {
  370.                             // Found a card in a slot
  371.                             
  372.                             numSlots++;
  373.                             EnumeratePropertiesForThisName(&entry, pathName);
  374.                             
  375.                         } else
  376.                             status = noErr;    // at present
  377.                     }
  378.                     
  379.                     if (pathName != NULL)
  380.                         DisposePtr((Ptr) pathName);
  381.                     
  382.                     myRegistryEntryIDDispose(&entry);
  383.                 }
  384.             }
  385.         
  386.             myRegistryEntryIterateDispose(&cookie);
  387.         }
  388.         
  389.     return (numSlots);
  390. }
  391.  
  392. //================================================================================================
  393. // Get PCI slot # for specified driver refNum, by searching NameRegistry for "driver-ref" properties.
  394. // assume slot #'s are of form $Xn (X = A, B, C, D, E, F; n = 1, 2), so we can return a "short"
  395. //================================================================================================
  396. short GetSlotForThisRefNum(short refNum)
  397. {
  398.     short                        slotNum;
  399.     
  400.     OSStatus                    status;
  401.     RegEntryIter            cookie;
  402.     Boolean                    done;
  403.     RegEntryIterationOp    op;
  404.     RegEntryID                entry;
  405.     RegCStrPathName        *pathName;
  406.     RegPathNameSize        pathNameSize;
  407.     RegPropertyNameBuf    foundProperty;
  408.     RegPropertyValueSize    propertySize;
  409. //    RegPropertyModifiers propertyModifiers;
  410.     void                        *propertyValue;
  411.  
  412.     slotNum = 0;
  413.     
  414.     op = kRegIterContinue;
  415.     status = myRegistryEntryIterateCreate(&cookie);
  416.     
  417.     if (status == noErr)
  418.     {
  419.         done = FALSE;
  420.         
  421.         while (status == noErr && done == FALSE)
  422.         {
  423.             status = myRegistryEntryIterate(&cookie, op, &entry, &done);
  424.             
  425.             if (status == noErr && done == FALSE)
  426.             {
  427.                 pathName = NULL;
  428.                 status = myRegistryEntryToPathSize(&entry, &pathNameSize);
  429.                 
  430.                 if (status == noErr)
  431.                 {
  432.                     pathName = (RegCStrPathName *) NewPtr(pathNameSize);
  433.                     
  434.                     if (pathName == NULL)
  435.                         status = MemError();
  436.                 }
  437.                 
  438.                 if (status == noErr)
  439.                 {
  440.                     status = myRegistryCStrEntryToPath(&entry, pathName, pathNameSize);
  441.                     //printf("%s\n", pathName);    // debugging
  442.                 }
  443.                 
  444.                 if (status == noErr)
  445.                 {
  446.                     propertyValue = NULL;
  447.                     propertySize = 0;
  448.                     getindstring((char *)foundProperty, kSlotPropertySTRID, kSlotPropertyName);
  449.                     //strcpy(foundProperty, "AAPL,slot-name");
  450.                     
  451.                     status = myRegistryPropertyGetSize(&entry, foundProperty, &propertySize);
  452.                     
  453.                     if (status == noErr)
  454.                     {
  455.                         propertyValue = NewPtr(propertySize);
  456.                         
  457.                         if (propertyValue == NULL)
  458.                         {
  459.                             status = MemError();
  460.                             //DebugStr((const unsigned char*)"\pNo memory to store property");
  461.                         }
  462.                     }
  463.                     
  464.                     if (status == noErr)
  465.                     {
  466.                         status = myRegistryPropertyGet(&entry, foundProperty, propertyValue, &propertySize);
  467.                                 
  468.                         // Slot names on Power Macintosh currently are of form : A1, B1, C1, D2, E2, F2
  469.                         // but older versions of drivers create slot-names like "mace" which are not what we are looking for
  470.                         
  471.                         if ((status == noErr) && (propertySize > 4))    // could check if slot name contains non-Hex digits
  472.                             status = -1;    // doesn't appear to be a valid slot name
  473.                     }
  474.                     
  475.                     if (status == noErr)
  476.                     {
  477.                         //sprintf(tempstr1, tempstr2, (char *)propertyValue, pathName);
  478.                         //sprintf(tempstr1, tempstr2, (char *)propertyValue);
  479.                         slotNum = *(short *)propertyValue;    // convert it to a short
  480.                     }
  481.                     
  482.                     if (propertyValue != NULL)
  483.                         DisposePtr((Ptr) propertyValue);
  484.                     
  485.                     if (!status)
  486.                     {
  487.                         // Found a card in a slot
  488.                         
  489.                         getindstring((char *)foundProperty, kSlotPropertySTRID, kSlotDriverRefNumItem);
  490.                         
  491.                         propertyValue = NULL;
  492.                         propertySize = 0;
  493.                         status = myRegistryPropertyGetSize(&entry, foundProperty, &propertySize);
  494.                         
  495.                         if (status == noErr)
  496.                         {
  497.                             propertyValue = NewPtr(propertySize);
  498.                             if (propertyValue == NULL)
  499.                             {
  500.                                 status = MemError();
  501.                                 //DebugStr((const unsigned char*)"\pNo memory to store property");
  502.                             }
  503.                         
  504.                             if (status == noErr)
  505.                             {
  506.                                 status = myRegistryPropertyGet(&entry, foundProperty, propertyValue, &propertySize);
  507.                             }
  508.                             
  509. #if 0
  510.                             if (status == noErr)
  511.                             {
  512.                                 status = myRegistryPropertyGetMod(&entry, foundProperty, &propertyModifiers);
  513.                             }
  514. #endif
  515.                             if (status == noErr)
  516.                             {
  517.                                 if (*(short *)propertyValue == refNum)
  518.                                 {
  519.                                     // we have found the correct driver, so we can stop
  520.                                     done = TRUE;
  521.                                 } else
  522.                                     slotNum = 0;    // reset it for the next slot
  523.                             }
  524.                             
  525.                             if (propertyValue != NULL)
  526.                                 DisposePtr((Ptr) propertyValue);
  527.                         } else
  528.                             status = noErr;    // there is a card but NO display attached (no open driver)
  529.                     } else
  530.                         status = noErr;    // not a slot device
  531.                 }
  532.                 
  533.                 if (pathName != NULL)
  534.                     DisposePtr((Ptr) pathName);
  535.                 
  536.                 myRegistryEntryIDDispose(&entry);
  537.             }
  538.         }
  539.     
  540.         myRegistryEntryIterateDispose(&cookie);
  541.     }
  542.     
  543.     return (slotNum);
  544. }    /* getslotforthisrefnum */
  545.  
  546. #ifdef __CFM68K__
  547. #pragma lib_export off
  548. #endif
  549.  
  550. #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
  551. #pragma options align=reset
  552. #endif
  553.  
  554. #ifdef __cplusplus
  555. }
  556. #endif
  557.